﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using BicycleTrafficSimulator.Presentation;
using BicycleTrafficSimulator.DataModel;
using System.Windows.Forms;

namespace BicycleTrafficSimulator
{
    public class Scene : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager graphics;

        public GraphicsDeviceManager Graphics
        {
            get { return graphics; }
            set { graphics = value; }
        }
        private SpriteBatch spriteBatch;

        public SpriteBatch SpriteBatchMenu
        {
            get { return spriteBatch; }
            set { spriteBatch = value; }
        }

        public enum GameState { StartMenu, Login, Loading, Playing, Instructions, Finished }
        public GameState gameState;

        public GameState GameStateScene
        {
            get { return gameState; }
            set { gameState = value; }
        }

        private Texture2D loadingScreen;

        Menu menu;
        LoginForm form;
        Game game;
        User user;
        InfoPage infoPage;
        InstructionPage instructionPage;
        private bool loadingReady = false;
        private bool fbPosted = false;

        public Scene()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            Components.Add(infoPage);
        }

        protected override void Initialize()
        {
            graphics.PreferredBackBufferWidth = 1400;
            graphics.PreferredBackBufferHeight = 780;
            //graphics.IsFullScreen = true;
            this.Window.AllowUserResizing = true;
            graphics.ApplyChanges();

            IsMouseVisible = true;

            GameStateScene = GameState.StartMenu;
            menu = new Menu(this);
            menu.Initialize();
            form = LoginForm.Instance;
            user = new User();
            instructionPage = new InstructionPage(this);
            instructionPage.Initialize();

            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            SpriteBatchMenu = new SpriteBatch(graphics.GraphicsDevice);
            loadingScreen = Content.Load<Texture2D>(@"loading");

            base.LoadContent();
        }

        protected override void UnloadContent()
        {

        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
                this.Exit();

            switch (gameState)
            {
                case (GameState.StartMenu):
                    menu.Update(gameTime);
                    break;

                case (GameState.Instructions):
                    instructionPage.Update(gameTime);
                    break;

                case (GameState.Login):
                    if (form.StartGame)
                    {
                        gameState = GameState.Loading;
                        form.StartGame = false;
                    }
                    break;

                case (GameState.Loading):
                    user = form.User;
                    if (loadingReady)
                    {
                        if (user.Age < 6)
                        {
                            game = new GameEasyLevel(this);
                            game.Initialize();
                            gameState = GameState.Playing;
                        }
                        else
                        {
                            game = new GameHardLevel(this);
                            game.Initialize();
                            gameState = GameState.Playing;
                        }
                        infoPage = new InfoPage(this, form.User);
                        infoPage.Initialize();
                        loadingReady = false;
                    }
                    break;

                case (GameState.Playing):

                    if (user.Age < 6)
                        game.Update(gameTime);

                    else
                        game.Update(gameTime);
                    user.Score = game.Score;
                    break;

                case (GameState.Finished):
                    infoPage.Update(gameTime);

                    if (infoPage.Logout)
                    {
                        user = null;
                        game.gameContent.Unload();
                    }
                    break;
            }

            base.Update(gameTime);
        }


        protected override void Draw(GameTime gameTime)
        {

            switch (gameState)
            {
                case (GameState.StartMenu):
                    GraphicsDevice.Clear(Color.White);
                    menu.Draw(gameTime);
                    GraphicsDevice.DepthStencilState = DepthStencilState.Default;
                    GraphicsDevice.BlendState = BlendState.Opaque;
                    break;

                case (GameState.Instructions):
                    GraphicsDevice.Clear(Color.White);
                    instructionPage.Draw(gameTime);
                    GraphicsDevice.DepthStencilState = DepthStencilState.Default;
                    GraphicsDevice.BlendState = BlendState.Opaque;
                    break;

                case (GameState.Login):
                    if (!form.ClosedBool)
                    {
                        form.Show();
                        form.TopMost = true;
                    }
                    else
                    {
                        gameState = GameState.StartMenu;
                        form.Hide();
                        form.ClosedBool = false;
                    }
                    break;

                case (GameState.Loading):
                    SpriteBatchMenu.Begin();
                    SpriteBatchMenu.Draw(loadingScreen, Vector2.Zero, Color.White);
                    SpriteBatchMenu.End();
                    loadingReady = true;
                    break;

                case (GameState.Playing):
                    if (user.Age < 6)
                        game.Draw(gameTime);
                    else
                        game.Draw(gameTime);
                    break;

                case (GameState.Finished):
                    user.Score.Value = form.UpdateScore(user);

                    GraphicsDevice.Clear(Color.White);
                    infoPage.Draw(gameTime);
                    GraphicsDevice.DepthStencilState = DepthStencilState.Default;
                    GraphicsDevice.BlendState = BlendState.Opaque;

                    if (infoPage.PostOnFb && !fbPosted)
                    {
                        string message = "My highest score is " + user.Score.Value.ToString() + ". Do you accept the challenge?";
                        form.PostToFeed(message);
                        fbPosted = true;
                        MessageBox.Show("Message has been posted.");
                    }
                    break;

            }

            base.Draw(gameTime);

        }

    }
}
